<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Request;
use think\Db;

class Order extends Api
{
    //列表数据
    public function index()
    {
        if($this->request->isPost())
        {
            $post = input('post.');
            $post = array_filter($post);
            $limit = $post['limit'] ?? 10;
            $page = $post['page'] ?? 1;
            if(!isset($post['user_id'])){
                return fh(404,'error','缺少用户id',[]);
            }
        	Db::startTrans();
            try{
	            if(isset($post['status'])){
                    $list = model($this->request->controller())->where([
                        ['user_id','=',$post['user_id']],
                        ['status','=',$post['status']]
                    ])->paginate($limit);
                    foreach ($list as $k=>$v){
                        if($v['status'] == 10){
                            $list[$k]['money'] = $list[$k]['product']['price']*$v['num'] ;
                        }
                    }
	            }else{
                    $list = model($this->request->controller())->where([
                        ['user_id','=',$post['user_id']],
                        ['status','<>',60]
                    ])->paginate($limit);
                    foreach ($list as $k=>$v){
                        if($v['status'] == 10){
                            $list[$k]['money'] = $list[$k]['product']['price']*$v['num'] ;
                        }
                    }
	            }
            	Db::commit();
            }catch (\Exception $e){
            	Db::rollback();
                return fh(500,'error',$e->getMessage(),[]);
            }
            return fh(200,'success','成功',$list);
        }
    }
    //订单添加
    public function add()
    {
        if($this->request->isPost())
        {
            $post = input('post.');
            $post = array_filter($post);
            if ($post){
                if(!isset($post['user_id']) || !isset($post['product_id']) || !isset($post['address_id']) || !isset($post['num'])){
                    return fh(1,'error','参数不全',[]);
                }
            Db::startTrans();
                try{
                    $user = model('user')->get($post['user_id'])->toArray();
                    $product = model('product')->where('id','=',$post['product_id'])->find()->toArray();
                    $post['buyer'] = $user['name'] ?? '无昵称';
                    $post['name'] = $product['name'] ?? '未命名产品';
                    $post['orderid'] = date('YmdHis') . rand(10000,99999);
                    $address = model('address')->where('id','=',$post['address_id'])->find()->toArray();
                    $post['phone'] = $address['phone'];
                    $post['address'] = $address['content'];
                    $post['addressee'] = $address['name'];
                    $model = model($this->request->controller())->save($post);
                    $orderid = model($this->request->controller())->id;
                    //添加操作记录
                    model('record')->save(['user_id'=>$post['user_id'],'title'=>'用户提交订单','order_id'=>$orderid]);
                Db::commit();
                }catch (\Exception $e){
                Db::rollback();
                    return fh(500,'error',$e->getMessage(),[]);
                }

                return fh(200,'success','添加成功',$model);
            }else{
                return fh(404,'error','没有post数据',[]);
            }
        }
    }
    //订单状态变更
    public function updete()
    {
        if($this->request->isPost())
        {
            $post = input('post.');
            if(!isset($post['status']) || !isset($post['order_id']))
            {
                return fh(1,'error','缺少参数',[]);
            }
            Db::startTrans();
            try{
                $model = model('order')->save(['status'=>$post['status']],['id'=>$post['order_id']]);
                //添加操作记录
                if($post['status'] == 20){
                    model('record')->save(['user_id'=>$post['user_id'],'title'=>'用户已付款','order_id'=>$post['order_id']]);
                }elseif ($post['status'] == 30){
                    model('record')->save(['user_id'=>$post['user_id'],'title'=>'平台已发货','order_id'=>$post['order_id']]);
                }elseif ($post['status'] == 40){
                    model('record')->save(['user_id'=>$post['user_id'],'title'=>'用户已收货','order_id'=>$post['order_id']]);
                }elseif ($post['status'] == 50){
                    model('record')->save(['user_id'=>$post['user_id'],'title'=>'用户取消订单','order_id'=>$post['order_id']]);
                }elseif ($post['status'] == 60){
                    model('record')->save(['user_id'=>$post['user_id'],'title'=>'用户删除订单','order_id'=>$post['order_id']]);
                }
                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
                return fh(500,'error',$e->getMessage(),[]);
            }
            return fh(200,'success','添加成功',$model);
        }
    }
    public function gm(){
        if($this->request->isPost())
        {
            $post = input('post.');
            $post = array_filter($post);
            if(!isset($post['user_id']) || !isset($post['id'])){
                return fh(404,'error','参数不全',[]);
            }
            Db::startTrans();
            try{
                $order = model('order')->where('id','=',$post['id'])->with('product')->find()->toArray();
                //库存减少
                //库存
                $num = $order['product']['stock']*1 - $order['num']*1;
                if($num < 0){
                    return fh(404,'error','库存不足',[]);
                }
                //销售
                $xsnum = $order['product']['sell']*1 + $order['num']*1;
                model('product')->save(['sell'=>$xsnum,'stock'=>$num],['id'=>$order['product']['id']]);
                if($order['status'] != 10){
                	return fh(404,'error','该订单不能进行付款' . $order['status'],[]);
                }
                //购物用户
                $user = model('user')->where('id','=',$post['user_id'])->find()->toArray();
                //直属上级
                if($user['parent_id'] == 0){
                    $pasmen = '';
                }else{
                    $pasmen = model('user')->where('id','=',$user['parent_id'])->find();
                    if($pasmen){
                        $pasmen = $pasmen->toArray();
                    }else{
                        $pasmen = '';
                    }
                }
                //订单数据
                $money = $order['product']['price'] * $order['num'];
                $money = round($money,2);
                //订单 待付款 转 待发货
                model('order')->save(['status'=>20,'money'=>$money],['id'=>$post['id']]);
                //添加操作记录
                model('record')->save(['user_id'=>$post['user_id'],'title'=>'用户付款','order_id'=>$post['id']]);
                //资产表数据增加
                if($user['relation'] == 1){
                    model('assets')->save(['user_id'=>$post['user_id'],'class'=>3,'relation'=>1,'money'=>$money]);
                }elseif($user['relation'] == 2){
                    model('assets')->save(['user_id'=>$post['user_id'],'class'=>3,'relation'=>2,'money'=>$money]);
                }elseif($user['relation'] == 3){
                    model('assets')->save(['user_id'=>$post['user_id'],'class'=>3,'relation'=>3,'money'=>$money]);
                }elseif($user['relation'] == 4){
                    model('assets')->save(['user_id'=>$post['user_id'],'class'=>3,'relation'=>2,'money'=>$money]);
                }
                //日对账表数据添加、修改
                $day = model('daily')->where('time','=',date('Y-m-d'))->find();
                if($day){
                    $day = $day->toArray();
                    //日收入
                    $daysr = $day['sr']*1 + $money*1;
                    //日收益
                    $daysy = $day['revenue']*1 + $money*1;
                    //日结算
                    $dayjs = $day['settlement']*1 + $money*1;
                    model('daily')->save(['sr'=>$daysr,'revenue'=>$daysy,'settlement'=>$dayjs],['id'=>$day['id']]);
                }else{
                    $zuih = model('daily')->order('id','desc')->find();
                    if($zuih){
                        $zuih = $zuih->toArray();
                        //日收入
                        $daysr = $day['sr']*1 + $money*1;
                        //日收益
                        $daysy = $day['revenue']*1 + $money*1;
                        //日结算
                        $dayjs = $day['settlement']*1 + $money*1;
                        model('daily')->save(['time'=>date('Y-m-d'),'sr'=>$money,'revenue'=>$dayjs,'settlement'=>$dayjs]);
                    }else{
                        model('daily')->save(['time'=>date('Y-m-d'),'sr'=>$money,'revenue'=>$money,'settlement'=>$money]);
                    }
                }
                //月对账表数据添加、修改
                $yue = model('month')->where('time','=',date('Y-m'))->find();
                if($yue){
                    $yue = $yue->toArray();
                    //月收入
                    $daysr = $yue['sr']*1 + $money*1;
                    //月收益
                    $daysy = $yue['revenue']*1 + $money*1;
                    //月结算
                    $dayjs = $yue['settlement']*1 + $money*1;
                    model('month')->save(['sr'=>$daysr,'revenue'=>$daysy,'settlement'=>$dayjs],['id'=>$yue['id']]);
                }else{
                    $zuih = model('month')->order('id','desc')->find();
                    if($zuih){
                        $zuih = $zuih->toArray();
                        //月收入
                        $daysr = $zuih['sr']*1 + $money*1;
                        //月收益
                        $daysy = $zuih['revenue']*1 + $money*1;
                        //月结算
                        $dayjs = $zuih['settlement']*1 + $money*1;
                        model('month')->save(['time'=>date('Y-m'),'sr'=>$daysr,'revenue'=>$dayjs,'settlement'=>$dayjs]);
                    }else{
                        model('month')->save(['time'=>date('Y-m'),'sr'=>$money,'revenue'=>$money,'settlement'=>$money]);
                    }
                }
                //进行产品购买的返现
                if($pasmen){
                    if($pasmen['ptsf'] !== 10 && $pasmen['ptsf'] !== 3){
                        //上级不是普通用户和普通产品代理可以得到返现30元
                        $pasmoney = model('wallet')->where('user_id','=',$pasmen['id'])->select();
                        if($pasmoney){
                            $pasmoney = $pasmoney->toArray();
                        }
                        $money = 0;
                        if($pasmoney){
                            foreach ($pasmoney as $v){
                                if($v['class'] == 1){
                                    $now = -$v['money'];
                                }else{
                                    $now = $v['money'];
                                }
                                $money = $money + $now;
                            }
                        }
                        //钱包收支明细表 添加 返现数据
                        $money = $money + 30;
                        model('wallet')->save(['user_id'=>$pasmen['id'],'status'=>1,'class'=>2,'money'=>30,'balance'=>$money]);
                    }
                }
                //查询所有产品代理商升级的条件
                $identitygong = model('identitygong')->select()->toArray();

                //判断自身是否提升等级
                //查询资产表，汇总购买清单
                $assets = model('assets')->where('user_id','=',$post['user_id'])->select()->toArray();
                $money = 0;
                foreach ($assets as $v){
                    if($v['class'] == 3){
                        $money = $money + $v['money'];
                    }
                }
                $nowtime = 99;
                if ($money >= 1000 && $money < 3000 && $user['ptsf'] == 10){
                    //变为普通会员
                    model('user')->save(['ptsf'=>1],['id'=>$post['user_id']]);
                    $nowtime = 1;
                }elseif($money >= 3000 && $money < 6000 && $user['ptsf'] == 10 || $user['ptsf'] == 1){
                    //变为VIP会员
                    model('user')->save(['ptsf'=>2],['id'=>$post['user_id']]);
                    $nowtime = 2;
                }elseif($money >= 6000 && $user['ptsf'] == 10 || $user['ptsf'] == 1 || $user['ptsf'] == 2){
                    //变为普通代理商
                    model('user')->save(['ptsf'=>3],['id'=>$post['user_id']]);
                    $nowtime = 3;
                }
                //当前用户查询直属和自身的下级资产表，汇总购买清单
                //当前用户拥有的直属下级
                $nowalluser = model('user')->where('parent_id','=',$user['id'])->select()->toArray();
                $nowalluserid = '';
                foreach ($nowalluser as $v){
                    $nowalluserid = $nowalluserid . $v['id'] . ',';
                }
                $nowalluserid = $nowalluserid . $user['id'];
                //当前用户查询直属和自身的下级资产表，汇总购买清单
                $nowassetsall = model('assets')->where('user_id','in',$nowalluserid)->select()->toArray();
                $money = 0;
                foreach ($nowassetsall as $v){
                    if($v['class'] == 3){
                        $money = $money + $v['money'];
                    }
                }
                if($money >= $identitygong[0]['performance'] && $money < $identitygong[1]['performance']){
                    //当前用户升级产品区代理商
                    if($user['canpinsf'] == 99){
                        model('user')->save(['canpinsf'=>4],['id'=>$user['id']]);
                        $nowtime = 4;
                    }
                }elseif($money >= $identitygong[1]['performance'] && $money < $identitygong[2]['performance']){
                    //当前用户升级产品市代理商
                    if($user['canpinsf'] == 99 || $user['canpinsf'] == 4){
                        //当前用户直属下级有两个产品区代理
                        $qnum = model('user')->where([
                            ['parent_id','=',$user['id']],
                            ['canpinsf','=',4]
                        ])->select();
                        if($qnum)
                        {
                            $qnum = count($qnum->toArray());
                            if($qnum >= 2){
                                model('user')->save(['canpinsf'=>5],['id'=>$user['id']]);
                                $nowtime = 5;
                            }
                        }
                    }
                }elseif($money >= $identitygong[2]['performance'] && $money < $identitygong[3]['performance']){
                    //当前用户升级产品省代理商
                    if($user['canpinsf'] == 99 || $user['canpinsf'] == 4 || $user['canpinsf'] == 5){
                        //当前用户直属下级有三个产品市代理
                        $qnum = model('user')->where([
                            ['parent_id','=',$user['id']],
                            ['canpinsf','=',5]
                        ])->select();
                        if($qnum)
                        {
                            $qnum = count($qnum->toArray());
                            if($qnum >= 3){
                                model('user')->save(['canpinsf'=>7],['id'=>$user['id']]);
                                $nowtime = 6;
                            }
                        }
                    }
                }
                //直属上级身份升级，达到产品代理等级或普通代理商等级后汇总下级的消费总额和自身的消费总额，判断是否升级身份
                if($pasmen){
                    if($pasmen['canpinsf'] !== 99 || $pasmen['ptsf'] == 3){
                        //直属上级拥有的直属下级
                        $alluser = model('user')->where('parent_id','=',$pasmen['id'])->select()->toArray();
                        $alluserid = '';
                        foreach ($alluser as $v){
                            $alluserid = $alluserid . $v['id'] . ',';
                        }
                        $alluserid = $alluserid . $pasmen['id'];
                        //上级查询直属和自身的下级资产表，汇总购买清单
                        $assetsall = model('assets')->where('user_id','in',$alluserid)->select()->toArray();
                        $money = 0;
                        foreach ($assetsall as $v){
                            if($v['class'] == 3){
                                $money = $money + $v['money'];
                            }
                        }

                        if($money >= $identitygong[0]['performance'] && $money < $identitygong[1]['performance']){
                            //升级产品区代理商
                            if($pasmen['canpinsf'] == 99){
                                model('user')->save(['canpinsf'=>4],['id'=>$pasmen['id']]);
                            }
                        }elseif($money >= $identitygong[1]['performance'] && $money < $identitygong[2]['performance']){
                            //升级产品市代理商
                            if($pasmen['canpinsf'] == 99 || $pasmen['canpinsf'] == 4){
                                //直属下级有两个产品区代理
                                $qnum = model('user')->where([
                                    ['parent_id','=',$pasmen['id']],
                                    ['canpinsf','=',4]
                                ])->select();
                                if($qnum)
                                {
                                    $qnum = count($qnum->toArray());
                                    if($qnum >= 2){
                                        model('user')->save(['canpinsf'=>5],['id'=>$pasmen['id']]);
                                    }
                                }
                            }
                        }elseif($money >= $identitygong[2]['performance'] && $money < $identitygong[3]['performance']){
                            //升级产品省代理商
                            if($pasmen['canpinsf'] == 99 || $pasmen['canpinsf'] == 4 || $pasmen['canpinsf'] == 5){
                                //直属下级有三个产品市代理
                                $qnum = model('user')->where([
                                    ['parent_id','=',$pasmen['id']],
                                    ['canpinsf','=',5]
                                ])->select();
                                if($qnum)
                                {
                                    $qnum = count($qnum->toArray());
                                    if($qnum >= 3){
                                        model('user')->save(['canpinsf'=>7],['id'=>$pasmen['id']]);
                                    }
                                }
                            }
                        }
                    }
                }

                //积分
                //当前用户自身消费获取积分
                //购物后获取对应身份的积分
                $order = model('order')->where('id','=',$post['id'])->find()->toArray();
                $user = model('user')->where('id','=',$user['id'])->find()->toArray();
                if($user['ptsf'] == 2){
                    $parent = model('user')->where([
                        ['parent_id','=',$user['id']],
                        ['ptsf','=',2]
                    ])->select();
                    if($parent){
                        $parent = count($parent->toArray());
                        if($parent = 1){
                            $jf = round($order['money']*0.002,2);
                            model('integral')->save(['user_id'=>$user['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$jf,'source'=>1,'status'=>1]);
                            $integralid = model('integral')->id;
                            jfmx($jf,$user['id'],$integralid);
                            $addjf = $jf;
                        }elseif($parent > 1){
                            $jf = round($order['money']*0.004,2);
                            model('integral')->save(['user_id'=>$user['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$jf,'source'=>1,'status'=>1]);
                            $integralid = model('integral')->id;
                            jfmx($jf,$user['id'],$integralid);
                            $addjf = $jf;
                        }
                    }
                }elseif ($user['ptsf'] == 3 && $user['canpinsf'] == 99){
                    //当前用户为普通代理商
                    $parent = model('user')->where([
                        ['parent_id','=',$user['id']],
                        ['ptsf','=',2]
                    ])->select();
                    if($parent){
                        $parent = count($parent->toArray());
                        if($parent == 1){
                            $jf = round($order['money']*1.5*0.002,2);
                            model('integral')->save(['user_id'=>$user['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$jf,'source'=>1,'status'=>1]);
                            $integralid = model('integral')->id;
                            jfmx($jf,$user['id'],$integralid);
                        }elseif($parent > 1){
                            $jf = round($order['money']*1.5*0.004,2);
                            model('integral')->save(['user_id'=>$user['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$jf,'source'=>1,'status'=>1]);
                            $integralid = model('integral')->id;
                            jfmx($jf,$user['id'],$integralid);
                        }
                    }
                }



                //直属上级获取积分
                if($pasmen){
                    if($pasmen['ptsf'] == 3 && $pasmen['canpinsf'] == 99){
                        //直属上级只是普通代理商
                        if($user['relation'] == 1){
                            //上级和当前用户不是平级关系，还在关联中
                            $jf = round($order['money']*$identitygong[0]['direct']*0.01,2);
                            model('integral')->save(['user_id'=>$pasmen['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$jf,'source'=>1,'status'=>1]);
                            $integralid = model('integral')->id;
                            jfmx($jf,$pasmen['id'],$integralid);
                        }elseif($user['relation'] == 4){
                            //上级和当前用户达成过平级关系，已经取消关联，但有平级奖励
                            $jf = round($order['money']*$identitygong[0]['direct']*0.0001,2);
                            model('integral')->save(['user_id'=>$pasmen['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$jf,'source'=>4,'status'=>1]);
                            $integralid = model('integral')->id;
                            jfmx($jf,$pasmen['id'],$integralid);
                        }
                    }elseif($pasmen['canpinsf'] == 4){
                        //直属上级为区代理商
                        if($user['relation'] == 1){
                            //上级和当前用户不是平级关系，还在关联中
                            //直推积分返佣
                            $jf = round($order['money']*$identitygong[1]['direct']*0.01,2);
                            model('integral')->save(['user_id'=>$pasmen['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$jf,'source'=>1,'status'=>1]);
                            $integralid = model('integral')->id;
                            jfmx($jf,$pasmen['id'],$integralid);
                            //额外代理积分返佣
                            $jf = round($order['money']*$identitygong[1]['division']*0.01,2);
                            model('integral')->save(['user_id'=>$pasmen['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$jf,'source'=>1,'status'=>1]);
                            $integralid = model('integral')->id;
                            jfmx($jf,$pasmen['id'],$integralid);
                            //直属上级的上级是否存在，并判断是否需要以此返回积分
                            $pasmensj = model('user')->where('id','=',$pasmen['parent_id'])->find();
                            if($pasmensj){
                                $pasmensj = $pasmensj->toArray();
                                if($pasmensj['canpinsf'] == 5){
                                    //上上级为市代理
                                    $jf = round($order['money']*($identitygong[2]['division']-$identitygong[0]['division'])*0.01,2);
                                    model('integral')->save(['user_id'=>$pasmensj['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$jf,'source'=>1,'status'=>1]);
                                    $integralid = model('integral')->id;
                                    jfmx($jf,$pasmensj['id'],$integralid);
                                }elseif($pasmensj['canpinsf'] == 7){
                                    //上上级为省代理
                                    $jf = round($order['money']*($identitygong[3]['division']-$identitygong[0]['division'])*0.01,2);
                                    model('integral')->save(['user_id'=>$pasmensj['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$jf,'source'=>1,'status'=>1]);
                                    $integralid = model('integral')->id;
                                    jfmx($jf,$pasmensj['id'],$integralid);
                                }
                            }
                        }elseif($user['relation'] == 4){
                            //上级和当前用户达成过平级关系，已经取消关联，但有平级奖励
                            //直推积分返佣
                            $jf = round($order['money']*$identitygong[1]['direct']*0.0001,2);
                            model('integral')->save(['user_id'=>$pasmen['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$jf,'source'=>4,'status'=>1]);
                            $integralid = model('integral')->id;
                            jfmx($jf,$pasmen['id'],$integralid);
                        }

                    }elseif($pasmen['canpinsf'] == 5){
                        if($user['relation'] == 1){
                            //上级和当前用户不是平级关系，还在关联中
                            //直属上级为市代理商
                            //直推积分返佣
                            $jf = round($order['money']*$identitygong[2]['direct']*0.01,2);
                            model('integral')->save(['user_id'=>$pasmen['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$jf,'source'=>1,'status'=>1]);
                            $integralid = model('integral')->id;
                            jfmx($jf,$pasmen['id'],$integralid);
                            //额外代理积分返佣
                            $jf = round($order['money']*$identitygong[2]['division']*0.01,2);
                            model('integral')->save(['user_id'=>$pasmen['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$jf,'source'=>1,'status'=>1]);
                            $integralid = model('integral')->id;
                            jfmx($jf,$pasmen['id'],$integralid);
                            //直属上级的上级是否存在，并判断是否需要以此返回积分
                            $pasmensj = model('user')->where('id','=',$pasmen['parent_id'])->find();
                            if($pasmensj){
                                $pasmensj = $pasmensj->toArray();
                                if($pasmensj['canpinsf'] == 7){
                                    //上上级为省代理
                                    $jf = round($order['money']*($identitygong[3]['division']-$identitygong[1]['division'])*0.01,2);
                                    model('integral')->save(['user_id'=>$pasmensj['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$jf,'source'=>1,'status'=>1]);
                                    $integralid = model('integral')->id;
                                    jfmx($jf,$pasmensj['id'],$integralid);
                                }
                            }
                        }elseif($user['relation'] == 4){
                            //上级和当前用户达成过平级关系，已经取消关联，但有平级奖励
                            $jf = round($order['money']*$identitygong[2]['direct']*0.0001,2);
                            model('integral')->save(['user_id'=>$pasmen['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$jf,'source'=>1,'status'=>1]);
                            $integralid = model('integral')->id;
                            jfmx($jf,$pasmen['id'],$integralid);
                        }
                    }elseif($pasmen['canpinsf'] == 7){
                        if($user['relation'] == 1){
                            //上级和当前用户不是平级关系，还在关联中
                            //直属上级为省代理商
                            //直推积分返佣
                            $jf = round($order['money']*$identitygong[3]['direct']*0.01,2);
                            model('integral')->save(['user_id'=>$pasmen['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$jf,'source'=>1,'status'=>1]);
                            $integralid = model('integral')->id;
                            jfmx($jf,$pasmen['id'],$integralid);
                            //额外代理积分返佣
                            $jf = round($order['money']*$identitygong[3]['division']*0.01,2);
                            model('integral')->save(['user_id'=>$pasmen['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$jf,'source'=>1,'status'=>1]);
                            $integralid = model('integral')->id;
                            jfmx($jf,$pasmen['id'],$integralid);
                        }elseif($user['relation'] == 4){
                            //上级和当前用户达成过平级关系，已经取消关联，但有平级奖励
                            $jf = round($order['money']*$identitygong[3]['direct']*0.0001,2);
                            model('integral')->save(['user_id'=>$pasmen['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$jf,'source'=>1,'status'=>1]);
                            $integralid = model('integral')->id;
                            jfmx($jf,$pasmen['id'],$integralid);
                        }
                    }
                }

                //市场互助积分
                //身份满足普通代理商
                if($user['ptsf'] == 3){
                    //上级拥有直属下级为普通代理商以上的数量
                    if($pasmen){
                    	$pasnum = model('user')->where([
	                        ['parent_id','=',$pasmen['id']],
	                        ['ptsf','=',3]
                    	])->select();
                    }else{
                    	$pasnum = false;
                    }
                    if($pasnum){
                        //上级的互助积分
                        $pasnum = count($pasnum->toArray());
                        if($pasnum == 2 && isset($addjf)){
                            $dqjf = $addjf*0.07;
                            model('integral')->save(['user_id'=>$pasmen['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$dqjf,'source'=>5,'status'=>1]);
                            $integralid = model('integral')->id;
                            jfmx($jf,$pasmen['id'],$integralid);//上上级的互助积分
                            if($pasmen['ptsf'] ==3){
                                $ssj = model('user')->where([
                                    ['id','=',$pasmen['parent_id']],
                                    ['ptsf','=',3]
                                ])->find();
                                if($ssj){
                                    $ssj = $ssj->toArray();
                                    $dqjf = $addjf*0.01;
                                    model('integral')->save(['user_id'=>$ssj['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$dqjf,'source'=>5,'status'=>1]);
                                    $integralid = model('integral')->id;
                                    jfmx($jf,$ssj['id'],$integralid);
                                }
                            }
                        }elseif($pasnum >= 3 && isset($addjf)){
                            $dqjf = $addjf*0.14;
                            model('integral')->save(['user_id'=>$pasmen['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$dqjf,'source'=>5,'status'=>1]);
                            $integralid = model('integral')->id;
                            jfmx($jf,$pasmen['id'],$integralid);//上上级的互助积分
                            if($pasmen['ptsf'] ==3){
                                $ssj = model('user')->where([
                                    ['id','=',$pasmen['parent_id']],
                                    ['ptsf','=',3]
                                ])->find();
                                if($ssj){
                                    $ssj = $ssj->toArray();
                                    $dqjf = $addjf*0.01;
                                    model('integral')->save(['user_id'=>$ssj['id'],'money'=>$order['money'],'order_id'=>$post['id'],'num'=>$dqjf,'source'=>5,'status'=>1]);
                                    $integralid = model('integral')->id;
                                    jfmx($jf,$ssj['id'],$integralid);
                                }
                            }
                        }
                    }
                }
                //relation 1关联中，2取消关联，3无关联，4平级奖励
                //平级奖励
                $user = model('user')->where('id','=',$user['id'])->find()->toArray();
                if($user['ptsf'] == 3){
                    //直属上级数据
                    $pasmen = model('user')->where([
                        ['id','=',$user['parent_id']],
                        ['ptsf','=',3]
                    ])->find();
                    if($pasmen){
                        $pasmen = $pasmen->toArray();
                        //上级和当期用户的身份等级相同
                        //取消他们之间的关联
                        if($user['canpinsf'] == $pasmen['canpinsf'] && $user['relation'] == 1){
                            model('user')->save(['relation'=>2],['id'=>$user['id']]);
                            $pageshu = model('user')->where([
                                ['parent_id','=',$pasmen['id']],
                                ['ptsf','=',3],
                                ['relation','=',2]
                            ])->select()->toArray();
                            //业绩清零
                            $yj = model('assets')->field('id,relation')->where('user_id','=',$user['id'])->select()->toArray();
                            foreach ($yj as $k=>$v){
                                $yj[$k]['relation'] = 2;
                            }
                            model('assets')->saveAll($yj);
                            //重新统计上级业绩
                            //查询还没脱离关系的下级
                            $pasall = model('user')->field('id')->where([
                                ['parent_id','=',$pasmen['id']],
                                ['relation','=',1]
                            ])->select();
                            if($pasall){
                                $pasall = $pasall->toArray();
                                $idall = implode(',',$pasall);
                                //查询还在关联中的用户的购买记录
                                $yj = model('assets')->field('money,moneyclass')->where([
                                    ['user_id','in',$idall],
                                    ['class','=',3]
                                ])->select()->toArray();
                                $money = 0;
                                foreach ($yj as $v){
                                    $money = $money + $v['money'];
                                }
                                //合计购买金额，刷新业绩
                                $money = round($money,2);
                                model('user')->save(['achievement'=>$money],['id'=>$pasmen['id']]);
                            }
                            $pageshuz = count($pageshu);
                            if($pageshuz >= 6){
                                //当前用户的上级拥有6个及以上平级的直属用户
                                //达成了平级奖励条件
                                //每次可以获得新增积分的1%
                                foreach ($pageshu as $k=>$v){
                                    $pageshu[$k]['relation'] = 4;
                                }
                                model('user')->save($pageshu);
                            }
                        }
                    }
                }
                //事务提交前再检测一次是否超出库存
                $order = model('order')->where('id','=',$post['id'])->with('product')->find()->toArray();
                //库存减少
                //库存
                if($order['product']['stock'] < 0){
                    Db::rollback();
                    return fh(404,'error','库存不足，购买失败',[]);
                }else{
                    Db::commit();
                }
            }catch (\Exception $e){
                return fh(500,'error','f3'.$e->getMessage(),[]);
            }  
            return fh(200,'success','暂时成功',[]);
        }
    }
}
